package com.qianren.edu.controller;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.octo.captcha.service.image.ImageCaptchaService;

@Controller
public class HomeController {
	private static final Logger logger = Logger.getLogger(HomeController.class);

	@Autowired
	private ImageCaptchaService imageCaptchaService;

	@Autowired
	private ResourceBundleMessageSource messageSource;

	/**
	 * 首页
	 * 
	 * @return
	 */
	@RequestMapping(value = "/")
	public String home() {

		return "index";
	}

	/**
	 * 验证码图片
	 * 
	 * @param session
	 * @param os
	 */
	@RequestMapping(value = "/verification")
	public void verification(HttpSession session, OutputStream os) {
		BufferedImage challenge = imageCaptchaService.getImageChallengeForID(session.getId());
		try {
			ImageIO.write(challenge, "JPEG", os);
		} catch (IOException e) {
			logger.error("create verification image ", e);
		}
	}

	/**
	 * 公司申请 - 页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/signup", method = RequestMethod.GET)
	public String signup() {
		return "signup";
	}

	/**
	 * 用户登出
	 * 
	 * @return
	 */
	@RequestMapping(value = "/logout")
	public String logout() {
		Subject currentUser = SecurityUtils.getSubject();
		currentUser.logout();
		return "redirect:/";
	}
}
